package main

import (
	"bigmodel/internal/types"
	"bigmodel/pkg/docx"
	"encoding/json"
	"flag"
	"fmt"
	"os"
)



func getData() string {
	return `
	[
        {
            "sentences":[
                {
                    "text":"文本数据",
                    "runProperties":{
                        "bold":false,
                        "size":"40",
                        "spacing":{
                            "lineRule":"auto",
                            "line":30,
                            "val":10
                        },
                        "underline":"double",
                        "highlight":"yellow",
                        "tab":true
                    }
                },
                {
                    "text":"第一航段落的第二组数据",
                    "runProperties":{
                        "bold":true,
                        "size":"40",
                        "spacing":{
                            "lineRule":"auto",
                            "line":30,
                            "val":10
                        },
                        "underline":"none",
                        "highlight":"green",
                        "tab":false,
                        "barterRabbet":true
                    }
                }
            ],
            "properties":{
                "justification":"center"
            }
        },
        {
            "sentences":[
                {
                    "text":"文本数据",
                    "runProperties":{
                        "bold":false,
                        "size":"40",
                        "spacing":{
                            "lineRule":"auto",
                            "line":30,
                            "val":10
                        },
                        "underline":"double",
                        "highlight":"yellow",
                        "tab":true
                    }
                },
                {
                    "text":"第二航段落的第二组数据",
                    "runProperties":{
                        "bold":true,
                        "size":"40",
                        "spacing":{
                            "lineRule":"auto",
                            "line":30,
                            "val":10
                        },
                        "underline":"none",
                        "highlight":"green",
                        "tab":false,
                        "barterRabbet":true
                    }
                }
            ],
            "properties":{
                "justification":"center"
            }
        }
    ]`
}

func main() {

	fileLocation := flag.String("f", "/Users/liangzhihao/companyCode/test/bigmodel/internal/text/new-file.docx", "file location")

	w := docx.New().WithDefaultTheme()
	strDt := getData()
	btDt := []byte(strDt)
	var dt []types.Paragraph
	err := json.Unmarshal(btDt, &dt)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Println(dt)
	for _, paragraph := range dt {
		p := w.AddParagraph()
		if paragraph.Properties != nil {
			if paragraph.Properties.TextAlignment != "" {
				p.TextAlignment(paragraph.Properties.TextAlignment)
			}
			if paragraph.Properties.Justification != "" {
				p.Justification(paragraph.Properties.Justification)
			}
			if paragraph.Sentences != nil {
				for _,sentence := range paragraph.Sentences {
					run := p.AddText(sentence.Text)
					if sentence.RunProperties != nil {
						if sentence.RunProperties.Bold {run.Bold()}
						if sentence.RunProperties.Color != ""{run.Color(sentence.RunProperties.Color)}
						if sentence.RunProperties.Size != "" {run.Size(sentence.RunProperties.Size)}
						if sentence.RunProperties.Highlight != "" {run.Highlight(sentence.RunProperties.Highlight)}
						if sentence.RunProperties.Tab {run.AddTab()}
						if sentence.RunProperties.Spacing != nil {
							run.Spacing(
								sentence.RunProperties.Spacing.Val,
								sentence.RunProperties.Spacing.Line,
								sentence.RunProperties.Spacing.Before,
								sentence.RunProperties.Spacing.BeforeLines,
								sentence.RunProperties.Spacing.LineRule,
							)
						}
					}
				}
			}
		}
	}
	f, err := os.Create(*fileLocation)
	if err != nil {
		panic(err)
	}
	_, err = w.WriteTo(f)
	if err != nil {
		panic(err)
	}
	defer f.Close()
}
